package com.device.operating.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.device.operating.entity.Department;
import com.device.operating.entity.Dprole;
import com.device.operating.entity.Role;
import com.device.operating.entity.User;
import com.device.operating.service.DepartmentService;
import com.device.operating.service.DproleService;
import com.device.operating.service.RoleService;
import com.device.operating.service.UserService;
import com.device.operating.utils.KeyProvider;
import com.device.operating.vo.Dptree;

@Controller
public class DepartmentController extends BaseController{
	
	@Resource(name="DepartmentService")
	private DepartmentService DepartmentService;
	
	@Resource(name="RoleService")
	private RoleService RoleService;
	
	@Resource(name="DproleService")
	private DproleService DproleService;
	
	@Resource(name="userService")
	private UserService userService;
	
	@RequestMapping("/todepartment")
	public String todepartment(ModelMap map){
		//List<Department> listdp=DepartmentService.querybyparentId("root");
		Department dproot=DepartmentService.queryone("root");
		map.put("dproot", dproot);
		return "org/department.jsp";
	}
	
	@RequestMapping("/toroles")
	public String torolesedit(){
		return "org/roles.jsp";
	}
	
	@RequestMapping("/showallroles")
	@ResponseBody
	public Map showallroles(){	
		Map map=new HashMap<>();
		List<Role> list=RoleService.queryall();
		map.put("rows", list);
		map.put("total", list.size());
		return map;
	}
	
	@RequestMapping("/updateroles")
	@ResponseBody
	public Map updateroles(HttpServletRequest request){
		Map map=new HashMap<>();		
		String listu=request.getParameter("listupdate");//更新
		String listd=request.getParameter("listdelete");//删除
		String listn=request.getParameter("listnew");//新增
		List<Role> listnew=null;
		if(!listn.equals("")){
			JSONArray ln=JSONArray.fromObject(listn);
			listnew=(List<Role>)JSONArray.toList(ln, Role.class);
		}		
		List<Role> listupdate=null;
		if(!listu.equals("")){
			JSONArray ln=JSONArray.fromObject(listu);
			listupdate=(List<Role>)JSONArray.toList(ln, Role.class);
		}
		
		List<Role> listdelete=null;
		if(!listd.equals("")){
			JSONArray ln=JSONArray.fromObject(listd);
			listdelete=(List<Role>)JSONArray.toList(ln, Role.class);
		}
		
		if(listnew!=null){
			for(Role role:listnew){
				role.setRoleId(KeyProvider.getPrimaryKey());
				RoleService.addrole(role);
			}
		}
		if(listupdate!=null){
			for(Role role:listupdate){
				RoleService.addrole(role);
			}
		}
		if(listdelete!=null){
			for(Role role:listdelete){
				RoleService.deleterole(role);
			}
		}
		map.put("msg", "操作成功!");
		return map;
	}
	
	@RequestMapping("/toloaddepartment")
	public String toloaddepartment(){
		return "org/loaddepartment.jsp";
	}
	
	@RequestMapping("/getrootdp")
	@ResponseBody
	public List<Dptree> getrootdp(){
		Dptree dt=new Dptree();
		List<Dptree> list=new ArrayList<Dptree>();
		List<Dptree> ltr=new ArrayList<Dptree>();
		Department dproot=DepartmentService.queryone("root");
		List<Department> ld1=DepartmentService.querybyparentId("root");
		for(Department depart:ld1){
			Dptree dptree=new Dptree();
			dptree.setId(depart.getDepartmentId());
			
			dptree.setText(depart.getDpname());
			if(DepartmentService.querybyparentId(depart.getDepartmentId()).size()>0){
				dptree.setState("closed");
				dptree.setChildren(null);
			}
			ltr.add(dptree);
		}
		dt.setId("root");
		dt.setState("closed");
		dt.setText(dproot.getDpname());
		dt.setChildren(ltr);
		list.add(dt);
		return list;
	}
	
	@RequestMapping("/extenddpnode")
	@ResponseBody
	public List<JSONObject> extenddpnode(HttpServletRequest request){
		List<JSONObject> listdt=new ArrayList<JSONObject>();
		String dpId=request.getParameter("nodeId");
		List<Department> ld1=DepartmentService.querybyparentId(dpId);
		for(Department depart:ld1){
			JSONObject json=new JSONObject();
			json.put("id", depart.getDepartmentId());
			json.put("text", depart.getDpname());
			if(DepartmentService.querybyparentId(depart.getDepartmentId()).size()>0){
				List<JSONObject> lj=new ArrayList<JSONObject>();
				json.put("state", "closed");
				json.put("children", lj);
				//dptree.setChildren(null);
			}
			listdt.add(json);
		}		
		return listdt;
	}
	
	@RequestMapping("/toadddepartment")
	public String toadddepartment(HttpServletRequest request,ModelMap map){
		String parentId=request.getParameter("parentId");
		Department dp=DepartmentService.queryone(parentId);
		map.put("department", dp);
		return "org/adddepartment.jsp";
	}
	
	@RequestMapping("/adddepartment")
	@ResponseBody
	public Map adddepartment(HttpServletRequest request){
		Map map=new HashMap();
		String parentId=request.getParameter("parentId");
		String parentname=request.getParameter("parentname");
		int parentlevel=new Integer(request.getParameter("parentlevel").toString()).intValue();
		int dplevel=parentlevel+1;
		String dpname=request.getParameter("dpname");
		String dptel=request.getParameter("dptel");
		String dploc=request.getParameter("dploc");
		String role=request.getParameter("roles");
		if(DepartmentService.querybynameandparentId(dpname, parentId).size()>0){
			map.put("msg", "有同名部门");
			return map;
		}
		String departmentId=KeyProvider.getPrimaryKey();
		String[] roles=role.split(",");
		for(int i=0;i<roles.length;i++){
			Dprole dprole=new Dprole();
			dprole.setDepartmentId(departmentId);
			dprole.setDpname(dpname);
			dprole.setDproleId(KeyProvider.getPrimaryKey());
			dprole.setRoleId(roles[i]);
			dprole.setRolename(RoleService.queryone(roles[i]).getRolename());
			DproleService.adddprole(dprole);
		}
		Department rootdp=DepartmentService.querydpls(parentId);
		Department dp=new Department();
		if(rootdp.getDepartmentId().equals("root")){
			dp.setRootdpId(departmentId);
			dp.setRootdpname(dpname);
		}else{
			dp.setRootdpId(rootdp.getDepartmentId());
			dp.setRootdpname(rootdp.getDpname());
		}
		dp.setDepartmentId(departmentId);
		dp.setDplevel(dplevel);
		dp.setDpname(dpname);
		dp.setDploc(dploc);
		dp.setDptel(dptel);
		dp.setParentId(parentId);
		DepartmentService.adddepartment(dp);
		map.put("msg", "添加成功");
		return map;
	}
	
	@RequestMapping(value="/deletedepartment",method=RequestMethod.POST)
	@ResponseBody
	public Map deletedepartment(HttpServletRequest request){
		String departmentId=request.getParameter("departmentId");
		Map map=DepartmentService.removedepartment(departmentId);
		return map;
	}
	
	@RequestMapping("/toeditdepartment")
	public String toeditdepartment(HttpServletRequest request,ModelMap map){
		String dpId=request.getParameter("departmentId");
		Department dp=DepartmentService.queryone(dpId);
		List<Role> listrole=RoleService.queryall();
		List<Dprole> listdprole=DproleService.querybydpId(dpId);
		List<User> listuser=userService.querybydepartmentId(dpId);
		map.put("listuser", listuser);
		map.put("dp", dp);
		map.put("listrole", listrole);
		map.put("listdprole", listdprole);
		return "org/editdepartment.jsp";
	}
	
	@RequestMapping("/getroles")
	@ResponseBody
	public List<Role> getroles(){
		return RoleService.queryall();
	}
	
	@RequestMapping("/getdproles")
	@ResponseBody
	public String[] getdproles(HttpServletRequest request){
		String dpId=request.getParameter("departmentId");
		List<Dprole> listdprole=DproleService.querybydpId(dpId);
		String[] dproles=new String[listdprole.size()];
		int i=0;
		for(Dprole dprole:listdprole){
			dproles[i++]=dprole.getRoleId();
		}
		return dproles;
	}
	
	@RequestMapping(value="/editdepartment",method=RequestMethod.POST)
	@ResponseBody
	public Map editdepartment(HttpServletRequest request){
		Map map=new HashMap<>();
		String dpId=request.getParameter("departmentId");
		String dpname=request.getParameter("dpname");
		String dptel=request.getParameter("dptel");
		String dploc=request.getParameter("dploc");
		String leaderId=request.getParameter("dpleader");
		String dproles=request.getParameter("dproles");

		String[] roles=dproles.split(",");
		List<Dprole> listdprole=new ArrayList<>();
		for(int i=0;i<roles.length;i++){
			Role role=RoleService.queryone(roles[i]);
			Dprole dprole=new Dprole();
			dprole.setDepartmentId(dpId);
			dprole.setDpname(dpname);
			dprole.setDproleId(roles[i]);
			dprole.setRoleId(roles[i]);
			dprole.setRolename(role.getRolename());
			dprole.setDproleId(KeyProvider.getPrimaryKey());
			listdprole.add(dprole);
		}
		DproleService.updatedproles(dpId, listdprole);
		Department dp=DepartmentService.queryone(dpId);
		User user=userService.queryUser(leaderId);
		dp.setDploc(dploc);
		dp.setDptel(dptel);
		dp.setLeaderId(leaderId);
		dp.setLeadername(user.getUsername());
		if(dpname.equals(dp.getDpname())){//名称未变
			DepartmentService.adddepartment(dp);
			map.put("msg", "编辑成功!");
			return map;
		}
		List<Department> listd=DepartmentService.quertbyname(dpname);
		if(listd.size()>0){
			map.put("msg", "名字重复!");
			return map; 
		}
		dp.setDpname(dpname);
		return DepartmentService.changedpname(dp);
	}
}
